<%@page import="javax.swing.text.StyledEditorKit.BoldAction"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="controller.ModuleLoginLogoutController, dto.*"%>
<% response.setContentType("text/html; charset=UTF-8");%>
<% request.setCharacterEncoding("utf-8");%>
<%
    ModuleLoginLogoutController ctrl = new ModuleLoginLogoutController();

    String cmnd = request.getParameter("cmnd");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    Object info = null;
    try {
        if (cmnd != null) {
            info = ctrl.loginThiSinh(cmnd);
        } else {
            info = ctrl.loginQuanTri(username, password);
        }
    } catch (Exception e) {
        e.getMessage();
    }

    // cookie initilization
    Cookie ckCMND = new Cookie("cmnd", "");
    Cookie ckUsername = new Cookie("username", "");
    Cookie ckPassword = new Cookie("password", "");

    if (info != null) {
        out.print("1"); // đăng nhập thành công
        HttpSession ss = request.getSession();
        if (cmnd != null) {
            ThiSinhDTO ts = (ThiSinhDTO) info;
            ss.setAttribute("type", "TS"); // thí sinh
            ss.setAttribute("maTS", ts.getMaTS());
            ss.setAttribute("hoten", ts.getHoten());
            if (request.getParameter("remember") != null) {
                ckCMND.setValue(cmnd);
                ckCMND.setMaxAge(604800); // 7 days
                response.addCookie(ckCMND);
            } else {
                ckCMND.setMaxAge(0); // delete cookie
                response.addCookie(ckCMND);
            }
        } else {
            QuanTriDTO qt = (QuanTriDTO) info;
            ss.setAttribute("type", "QT"); // quản trị
            ss.setAttribute("id", qt.getId());
            ss.setAttribute("username", qt.getUsername());
            if (request.getParameter("remember") != null) {
                ckUsername.setValue(username);
                ckUsername.setMaxAge(604800); // 7 days
                response.addCookie(ckUsername);
                ckPassword.setValue(password);
                ckPassword.setMaxAge(604800); // 7 days
                response.addCookie(ckPassword);
            } else {
                ckUsername.setMaxAge(0); // delete cookie
                response.addCookie(ckUsername);
                ckPassword.setMaxAge(0); // delete cookie
                response.addCookie(ckPassword);
            }
        }
        ss.setAttribute("isLogin", true);
    } else {
        // login failed
        out.print("0"); // tài khoản không đúng
        if (cmnd != null) {
            ckCMND.setMaxAge(0); // delete cookie
            response.addCookie(ckCMND);
        } else {
            ckUsername.setMaxAge(0); // delete cookie
            response.addCookie(ckUsername);
            ckPassword.setMaxAge(0); // delete cookie
            response.addCookie(ckPassword);
        }
    }
%>